﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Illusion;
using Illusion.CustomAttributes;
using UnityEngine;
using UnityEngine.Audio;

namespace Manager
{
	// Token: 0x02000509 RID: 1289
	public sealed class Voice : Singleton<Voice>
	{
		// Token: 0x170003E1 RID: 993
		// (get) Token: 0x060020DE RID: 8414 RVA: 0x000CA7B0 File Offset: 0x000C89B0
		public static AudioMixer Mixer
		{
			get
			{
				return Sound.Mixer;
			}
		}

		// Token: 0x060020DF RID: 8415 RVA: 0x000CA7B8 File Offset: 0x000C89B8
		protected override void Awake()
		{
			if (!base.CheckInstance())
			{
				return;
			}
			UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
			this.rootSetting = new GameObject("SettingObjectPCM").transform;
			this.rootSetting.SetParent(base.transform, false);
			this.rootPlay = new GameObject("PlayObjectPCM").transform;
			this.rootPlay.SetParent(base.transform, false);
			this.settingObjects = new GameObject[Utils.Enum<Voice.Type>.Length];
			for (int i = 0; i < this.settingObjects.Length; i++)
			{
				this.LoadSetting((Voice.Type)i, -1);
			}
			this.dicASCache = new Dictionary<int, Dictionary<string, AudioSource>>();
			this.ASCacheRoot = new GameObject("AudioSourceCache").transform;
			this.ASCacheRoot.SetParent(this.rootPlay, false);
			Transform transform = new GameObject("c00").transform;
			transform.SetParent(this.rootPlay, false);
			this.voiceDic.Add(0, transform);
		}

		// Token: 0x060020E0 RID: 8416 RVA: 0x000CA8B8 File Offset: 0x000C8AB8
		private void OnDestroy()
		{
			UnityEngine.Object.Destroy(this.rootSetting.gameObject);
			UnityEngine.Object.Destroy(this.rootPlay.gameObject);
			this.voiceDic.Clear();
		}

		// Token: 0x060020E1 RID: 8417 RVA: 0x000CA8E8 File Offset: 0x000C8AE8
		private void LoadSetting(Voice.Type type, int settingNo = -1)
		{
			string text = type.ToString();
			AssetBundleData assetBundleData = new AssetBundleData("sound/setting/object/00.unity3d", text.ToLower());
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(assetBundleData.GetAsset<GameObject>(), this.rootSetting, false);
			gameObject.name = text + "_Setting";
			AudioSource component = gameObject.GetComponent<AudioSource>();
			Singleton<Sound>.Instance.AudioSettingData(component, settingNo);
			this.settingObjects[(int)type] = gameObject;
			assetBundleData.UnloadBundle(true, false);
		}

		// Token: 0x060020E2 RID: 8418 RVA: 0x000CA960 File Offset: 0x000C8B60
		public void Bind(LoadVoice script)
		{
			if (script.audioSource == null)
			{
				Transform parent;
				if (!this.voiceDic.TryGetValue(script.no, out parent))
				{
					return;
				}
				Singleton<Sound>.Instance.SetParent(parent, script, this.settingObjects[(int)script.type]);
			}
			AudioSource audioSource = script.audioSource;
			audioSource.clip = script.clip;
			Singleton<Sound>.Instance.Register(script.clip);
			audioSource.name = audioSource.clip.name;
			Sound.OutputSettingData outputSettingData = Singleton<Sound>.Instance.AudioSettingData(audioSource, script.settingNo);
			if (outputSettingData != null)
			{
				script.delayTime = outputSettingData.delayTime;
			}
		}

		// Token: 0x060020E3 RID: 8419 RVA: 0x000CAA0C File Offset: 0x000C8C0C
		public List<AudioSource> GetPlayingList(int no)
		{
			List<AudioSource> list = new List<AudioSource>();
			Transform transform;
			if (!this.voiceDic.TryGetValue(no, out transform))
			{
				return list;
			}
			for (int i = 0; i < transform.childCount; i++)
			{
				list.Add(transform.GetChild(i).GetComponent<AudioSource>());
			}
			return list;
		}

		// Token: 0x060020E4 RID: 8420 RVA: 0x000CAA60 File Offset: 0x000C8C60
		public bool IsVoiceCheck(int no)
		{
			Transform transform;
			return this.voiceDic.TryGetValue(no, out transform) && transform.childCount != 0;
		}

		// Token: 0x060020E5 RID: 8421 RVA: 0x000CAA90 File Offset: 0x000C8C90
		public bool IsVoiceCheck(Transform voiceTrans)
		{
			foreach (Transform transform in from v in this.voiceDic.Values
			select v.transform)
			{
				for (int i = 0; i < transform.childCount; i++)
				{
					LoadVoice componentInChildren = transform.GetChild(i).GetComponentInChildren<LoadVoice>();
					if (componentInChildren.voiceTrans == voiceTrans)
					{
						return true;
					}
				}
			}
			return false;
		}

		// Token: 0x060020E6 RID: 8422 RVA: 0x000CAB4C File Offset: 0x000C8D4C
		public bool IsVoiceCheck(int no, Transform voiceTrans)
		{
			Transform transform;
			if (!this.voiceDic.TryGetValue(no, out transform))
			{
				return false;
			}
			for (int i = 0; i < transform.childCount; i++)
			{
				LoadVoice componentInChildren = transform.GetChild(i).GetComponentInChildren<LoadVoice>();
				if (componentInChildren.voiceTrans == voiceTrans)
				{
					return true;
				}
			}
			return false;
		}

		// Token: 0x060020E7 RID: 8423 RVA: 0x000CABA8 File Offset: 0x000C8DA8
		public bool IsVoiceCheck()
		{
			return this.voiceDic.Values.Any((Transform v) => v.childCount != 0);
		}

		// Token: 0x060020E8 RID: 8424 RVA: 0x000CABD8 File Offset: 0x000C8DD8
		public Transform Play(int no, string assetBundleName, string assetName, float pitch = 1f, float delayTime = 0f, float fadeTime = 0f, bool isAsync = true, Transform voiceTrans = null, Voice.Type type = Voice.Type.PCM, int settingNo = -1, bool isPlayEndDelete = true, bool isBundleUnload = true)
		{
			LoadVoice loadVoice = new GameObject("Voice Loading").AddComponent<LoadVoice>();
			loadVoice.no = no;
			loadVoice.assetBundleName = assetBundleName;
			loadVoice.assetName = assetName;
			loadVoice.pitch = pitch;
			loadVoice.delayTime = delayTime;
			loadVoice.fadeTime = fadeTime;
			loadVoice.isAsync = isAsync;
			loadVoice.voiceTrans = voiceTrans;
			loadVoice.type = type;
			loadVoice.settingNo = settingNo;
			loadVoice.isPlayEndDelete = isPlayEndDelete;
			loadVoice.isBundleUnload = isBundleUnload;
			Transform parent;
			if (!this.voiceDic.TryGetValue(no, out parent))
			{
				return null;
			}
			return Singleton<Sound>.Instance.SetParent(parent, loadVoice, this.settingObjects[(int)type]);
		}

		// Token: 0x060020E9 RID: 8425 RVA: 0x000CAC7C File Offset: 0x000C8E7C
		public Transform OnecePlay(int no, string assetBundleName, string assetName, float pitch = 1f, float delayTime = 0f, float fadeTime = 0f, bool isAsync = true, Transform voiceTrans = null, Voice.Type type = Voice.Type.PCM, int settingNo = -1, bool isPlayEndDelete = true, bool isBundleUnload = true)
		{
			this.StopAll();
			return this.Play(no, assetBundleName, assetName, pitch, delayTime, fadeTime, isAsync, voiceTrans, type, settingNo, isPlayEndDelete, isBundleUnload);
		}

		// Token: 0x060020EA RID: 8426 RVA: 0x000CACAC File Offset: 0x000C8EAC
		public Transform OnecePlayChara(int no, string assetBundleName, string assetName, float pitch = 1f, float delayTime = 0f, float fadeTime = 0f, bool isAsync = true, Transform voiceTrans = null, Voice.Type type = Voice.Type.PCM, int settingNo = -1, bool isPlayEndDelete = true, bool isBundleUnload = true)
		{
			if (voiceTrans != null)
			{
				this.Stop(no, voiceTrans);
			}
			else
			{
				this.Stop(no);
			}
			return this.Play(no, assetBundleName, assetName, pitch, delayTime, fadeTime, isAsync, voiceTrans, type, settingNo, isPlayEndDelete, isBundleUnload);
		}

		// Token: 0x060020EB RID: 8427 RVA: 0x000CACF8 File Offset: 0x000C8EF8
		public void StopAll()
		{
			List<GameObject> list = new List<GameObject>();
			foreach (Transform transform in from v in this.voiceDic.Values
			select v.transform)
			{
				for (int i = 0; i < transform.childCount; i++)
				{
					Transform child = transform.GetChild(i);
					list.Add(child.gameObject);
				}
			}
			list.ForEach(delegate(GameObject p)
			{
				UnityEngine.Object.Destroy(p);
			});
		}

		// Token: 0x060020EC RID: 8428 RVA: 0x000CADC8 File Offset: 0x000C8FC8
		public void Stop(int no)
		{
			Transform transform;
			if (!this.voiceDic.TryGetValue(no, out transform))
			{
				return;
			}
			List<GameObject> list = new List<GameObject>();
			for (int i = 0; i < transform.childCount; i++)
			{
				list.Add(transform.GetChild(i).gameObject);
			}
			list.ForEach(delegate(GameObject p)
			{
				UnityEngine.Object.Destroy(p);
			});
		}

		// Token: 0x060020ED RID: 8429 RVA: 0x000CAE3C File Offset: 0x000C903C
		public void Stop(Transform voiceTrans)
		{
			List<GameObject> list = new List<GameObject>();
			foreach (Transform transform in from v in this.voiceDic.Values
			select v.transform)
			{
				for (int i = 0; i < transform.childCount; i++)
				{
					Transform child = transform.GetChild(i);
					LoadVoice componentInChildren = child.GetComponentInChildren<LoadVoice>();
					if (componentInChildren.voiceTrans == voiceTrans)
					{
						list.Add(child.gameObject);
					}
				}
			}
			list.ForEach(delegate(GameObject p)
			{
				UnityEngine.Object.Destroy(p);
			});
		}

		// Token: 0x060020EE RID: 8430 RVA: 0x000CAF28 File Offset: 0x000C9128
		public void Stop(int no, Transform voiceTrans)
		{
			Transform transform;
			if (!this.voiceDic.TryGetValue(no, out transform))
			{
				return;
			}
			List<GameObject> list = new List<GameObject>();
			for (int i = 0; i < transform.childCount; i++)
			{
				Transform child = transform.GetChild(i);
				LoadVoice componentInChildren = child.GetComponentInChildren<LoadVoice>();
				if (componentInChildren.voiceTrans == voiceTrans)
				{
					list.Add(child.gameObject);
				}
			}
			List<GameObject> list2 = list;
			if (Voice.callback0 == null)
			{
				Voice.callback0 = new Action<GameObject>(UnityEngine.Object.Destroy);
			}
			list2.ForEach(Voice.callback0);
		}

		// Token: 0x060020EF RID: 8431 RVA: 0x000CAFB8 File Offset: 0x000C91B8
		public AudioSource CreateCache(int voiceNo, string bundle, string asset, string manifest = null)
		{
			Dictionary<string, AudioSource> dictionary;
			if (!this.dicASCache.TryGetValue(voiceNo, out dictionary))
			{
				Dictionary<string, AudioSource> dictionary2 = new Dictionary<string, AudioSource>();
				this.dicASCache[voiceNo] = dictionary2;
				dictionary = dictionary2;
			}
			AudioSource component;
			if (!dictionary.TryGetValue(asset, out component))
			{
				GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.settingObjects[0], this.ASCacheRoot, false);
				gameObject.name = asset;
				gameObject.SetActive(true);
				component = gameObject.GetComponent<AudioSource>();
				component.clip = AssetBundleManager.LoadAsset(bundle, asset, typeof(AudioClip), manifest).GetAsset<AudioClip>();
				Singleton<Sound>.Instance.Register(component.clip);
				dictionary.Add(asset, component);
			}
			return component;
		}

		// Token: 0x060020F0 RID: 8432 RVA: 0x000CB05C File Offset: 0x000C925C
		public void ReleaseCache(int voiceNo, string bundle, string asset, string manifest = null)
		{
			Dictionary<string, AudioSource> dictionary;
			if (!this.dicASCache.TryGetValue(voiceNo, out dictionary))
			{
				return;
			}
			AudioSource audioSource;
			if (dictionary.TryGetValue(asset, out audioSource))
			{
				Singleton<Sound>.Instance.Remove(audioSource.clip);
				UnityEngine.Object.Destroy(audioSource.gameObject);
				dictionary.Remove(asset);
				AssetBundleManager.UnloadAssetBundle(bundle, false, manifest, false);
			}
			if (!dictionary.Any<KeyValuePair<string, AudioSource>>())
			{
				this.dicASCache.Remove(voiceNo);
			}
		}

		// Token: 0x0400226B RID: 8811
		[NotEditable]
		[SerializeField]
		private Transform rootSetting;

		// Token: 0x0400226C RID: 8812
		[NotEditable]
		[SerializeField]
		private Transform rootPlay;

		// Token: 0x0400226D RID: 8813
		[SerializeField]
		[NotEditable]
		private Transform ASCacheRoot;

		// Token: 0x0400226E RID: 8814
		[NotEditable]
		[SerializeField]
		private GameObject[] settingObjects;

		// Token: 0x0400226F RID: 8815
		private Dictionary<int, Transform> voiceDic = new Dictionary<int, Transform>();

		// Token: 0x04002270 RID: 8816
		private Dictionary<int, Dictionary<string, AudioSource>> dicASCache;

		// Token: 0x04002278 RID: 8824
		[CompilerGenerated]
		private static Action<GameObject> callback0;

		// Token: 0x0200050A RID: 1290
		public enum Type
		{
			// Token: 0x0400227A RID: 8826
			PCM
		}
	}
}
